import { fileIo as fs } from '@kit.CoreFileKit';
import { util } from '@kit.ArkTS';

export function join(...paths: string[]): string {
  return paths.join('/')
}

export const readFile= async (filePath:string, encode?:string): Promise<any>=>{
  if (encode && encode.toLowerCase()==='utf8') {
    encode = 'utf-8'
  }
  let isExist = fs.accessSync(filePath);
  if (!isExist) {
    throw new Error(`File ${filePath} does not exist`);
  }
  const file = await fs.open(filePath, fs.OpenMode.READ_WRITE);
  const fileStat = await fs.stat(filePath);
  const fileSize = fileStat.size;
  let arrayBuffer = new ArrayBuffer(fileSize);
  await fs.read(file.fd, arrayBuffer);
  await fs.close(file.fd);

  const textDecoder = util.TextDecoder.create(encode)
  const result = textDecoder.decodeToString(new Uint8Array(arrayBuffer));
  return result
}